home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / Matrix.h < prev    next >
C/C++ Source or Header  |  1990-05-15  |  3KB  |  109 lines

  1. #ifndef MatrixH
  2. #define MatrixH
  3.  
  4. // Matrix.h -- matrix of type double
  5.  
  6. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/Matrix.h,v 3.0 90/05/15 22:43:53 kgorlen Rel $
  7.  
  8. class ostream;
  9. class MatrixCol;
  10. class MatrixRow;
  11.  
  12. typedef double diagonal;
  13.  
  14. class Matrix {
  15. protected:
  16.     int nrow, ncol;
  17.  
  18.     double* _v;
  19.     void v_alloc(int,int);
  20. public:
  21.     Matrix(int nr =1,int nc =1,double* =0);
  22.     Matrix(const Matrix&);
  23.     Matrix(const MatrixRow&);
  24.     Matrix(const MatrixCol&);
  25.     Matrix(int,double*);
  26.     Matrix(int,diagonal);
  27.     ~Matrix();
  28.  
  29.     double& at(int irow,int icol) const
  30.       { return *(_v+irow*(ncol)+icol); }
  31. //    double& at(int i) const { return at(i,i); }
  32.     double& operator()(int irow,int icol) const;
  33.     MatrixRow row(int) const;
  34.     MatrixRow row(int,const MatrixRow&) const;
  35.     MatrixCol col(int) const;
  36.     MatrixCol col(int,const MatrixCol&) const;
  37.  
  38.     int sameSize(int a,int b) const { return (nrow==a&&ncol==b); }
  39.     int nRow() const { return nrow; }
  40.     int nCol() const { return ncol; }
  41.     void operator=(const Matrix&);
  42.     int operator==(const Matrix&) const;
  43.  
  44.     Matrix t() const;  // transpose
  45.     void operator*=(double);
  46.  
  47.     void switchRows(int,int);
  48.     void switchCols(int,int);
  49.     void combineRows(int i,double b,int j); // row(i) += b*row(j)
  50.     void combineCols(int i,double b,int j); // col(i) += b*col(j)
  51.  
  52.     int isUpperTriangle() const;
  53.     Matrix upperTriangle();
  54.     Matrix coFactor(int,int) const;
  55.     Matrix coFactor(int,int,Matrix&) const;
  56.     Matrix operator~() const; //  inverse matrix, if any
  57.     
  58.     virtual void printOn(ostream&) const;
  59.     virtual void dumpOn(ostream&) const;
  60.     static void sizeError(char* where,const Matrix&,int,int);
  61. };
  62.  
  63. //determinent
  64. extern double det(const Matrix&);
  65. extern double norm(const Matrix&);
  66. extern Matrix operator+(const Matrix&,const Matrix&);
  67. extern Matrix operator-(const Matrix&,const Matrix&);
  68. extern Matrix operator*(const Matrix&,const Matrix&);
  69. extern Matrix operator-(const Matrix&);
  70. // concatenate columns
  71. extern Matrix operator&(const Matrix&,const Matrix&);
  72. // scalar multiply    
  73. extern Matrix operator*(double,const Matrix&);
  74.     
  75. class MatrixCol {
  76.     Matrix* pm;
  77.     int _col;    // index of this column
  78. public:
  79.     MatrixCol(int,const Matrix&);
  80.     MatrixCol(const MatrixCol&);
  81.     int nRow() const { return pm->nRow(); }
  82.     double& at(int i) const { return pm->at(i,_col); }
  83.     double& operator()(int i) const
  84.       { return pm->operator()(i,_col); }
  85.     void operator=(const MatrixCol&);
  86.     void operator+=(const MatrixCol&);
  87.     virtual void printOn(ostream&) const;
  88. };
  89.  
  90. class MatrixRow {
  91.     Matrix* pm;
  92.     int _row;    // index of this row
  93. public:
  94.     MatrixRow(int,const Matrix&);
  95.     MatrixRow(const MatrixRow&);
  96.     int nCol() const { return pm->nCol(); }
  97.     double& at(int i) const { return pm->at(_row,i); }
  98.     double& operator()(int i) const
  99.       { return pm->operator()(_row,i); }
  100.     double operator^(const MatrixCol&) const; // dot product
  101.     void operator=(const MatrixRow&);
  102.     virtual void printOn(ostream&) const;
  103. };
  104.  
  105. extern ostream& operator<<(ostream& strm,const Matrix& m);
  106. extern ostream& operator<<(ostream& strm,const MatrixRow& m);
  107. extern ostream& operator<<(ostream& strm,const MatrixCol& m);
  108. #endif/*MatrixH*/
  109.